# Installation Sources
########################################

### Mirror settings
d-i mirror/country string manual
d-i mirror/http/hostname string ftp.ch.debian.org
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string
d-i mirror/suite string stable

# security
d-i apt-setup/services-select multiselect security, updates
d-i apt-setup/security_host string security.debian.org

# non-free and contrib
d-i apt-setup/non-free boolean true
d-i apt-setup/contrib boolean true

# Networking
########################################

# Network Configuration
d-i netcfg/disable_dhcp boolean false
d-i netcfg/choose_interface select auto

d-i netcfg/get_hostname installbox
d-i netcfg/get_domain seen domain.example

# udeb
#######################################

d-i preseed/early_command string \
  anna-install fdisk-udeb

# Disk Partitioning/Boot loader
########################################

# Disk Partitioning/Boot loader
########################################

# automatically partition without confirmation
# remove existing partitions on first disk (/dev/sda)
d-i partman/early_command string \
  sdadisk=$(ls /dev | grep 'sda'); \
  nvmedisk=$(ls /dev | grep 'nvme0n1'); \
  $emmcdisk=$(ls /dev | grep 'mmcblk0'); \
  if [ "$nvmedisk" ]; \
  then \
    for i in 1 2 3 4 5 6 7 8; \
    do \
      if [ "$(fdisk -l /dev/nvme0n1 | grep /dev/nvme0n1p$i)" ]; \
      then \
        echo -e "d\n$i\nw\n" | fdisk /dev/nvme0n1; \
      fi; \
    done; \
    debconf-set partman-auto/disk "/dev/nvme0n1"; \
    debconf-set grub-installer/bootdev "/dev/nvme0n1"; \
  elif [ "$sdadisk" ]; \
  then \
    for i in 1 2 3 4 5 6 7 8; \
    do \
      if [ "$(fdisk -l /dev/sda | grep /dev/sda$i)" ]; \
      then \
        echo -e "d\n$i\nw\n" | fdisk /dev/sda; \
      fi; \
    done; \
    debconf-set partman-auto/disk "/dev/sda"; \
    debconf-set grub-installer/bootdev "/dev/sda"; \
  elif [ "$emmcdisk" ]; \
  then \
    for i in 1 2 3 4 5 6 7 8; \
    do \
      if [ "$(fdisk -l /dev/mmcblk0 | grep /dev/mmcblk0p$i)" ]; \
      then \
        echo -e "d\n$i\nw\n" | fdisk /dev/mmcblk0; \
      fi; \
    done; \
    debconf-set partman-auto/disk "/dev/mmcblk0"; \
    debconf-set grub-installer/bootdev "/dev/mmcblk0"; \
  fi; \
  exit 0;

d-i partman-auto/method string regular

d-i partman-lvm/device_remove_lvm boolean true
d-i partman-md/device_remove_md boolean true
d-i partman-lvm/confirm boolean true

# Keep that one set to true so we end up with a UEFI enabled
# system. If set to false, /var/lib/partman/uefi_ignore will be touched
d-i partman-efi/non_efi_system boolean true

# enforce usage of GPT - a must have to use EFI!
d-i partman-basicfilesystems/choose_label string gpt
d-i partman-basicfilesystems/default_label string gpt
d-i partman-partitioning/choose_label string gpt
d-i partman-partitioning/default_label string gpt
d-i partman/choose_label string gpt
d-i partman/default_label string gpt


# create a third partition which will be deleted afterwards in order
# to make sure that the root partition will not exceed 30GB
# this is necessary as d-i does not allow to have empty space left
d-i partman-auto/expert_recipe string            \
  efi-swap-root-spare ::                         \
    1 1 1 free                                   \
      $bios_boot{ }                              \
      method{ biosgrub }                         \
      .                                          \
    538 538 1075 free                            \
      $iflabel{ gpt }                            \
      $reusemethod{ }                            \
      method{ efi }                              \
      format{ }                                  \
      .                                          \
    512 1024 2048 linux-swap                     \
      $primary{ }                                \
      method{ swap } format{ }                   \
      .                                          \
    5000 10000 15000 ext4                        \
      $primary{ }                                \
      method{ format } format{ }                 \
      use_filesystem{ } filesystem{ ext4 }       \
      mountpoint{ / }                            \
      .                                          \
    100 1000 1000000000 ext4                     \
      $primary{ }                                \
      .                                          \

d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true

# grub 
d-i grub-installer/with_other_os boolean true
d-i grub-installer/only_debian boolean true

# suppress installation finished message
d-i finish-install/reboot_in_progress note

# don't eject cdrom after the installation has finished
d-i cdrom-detect/eject boolean false

# Localizations
########################################

# keyboard configuration during the installation 
d-i debian-installer/locale string en_US.UTF-8
#d-i keyboard-configuration/xkb-keymap select ch 

# timezone
d-i tzconfig/gmt boolean true
d-i tzconfig/choose_country_zone/Europe select Zurich
d-i tzconfig/choose_country_zone_single boolean true
d-i time/zone select Europe/Zurich
d-i clock-setup/utc boolean false

d-i clock-setup/ntp boolean true
d-i clock-setup/ntp-server 0.ch.pool.ntp.org

# User Creation
########################################

# Root User
d-i passwd/root-login boolean false
#d-i passwd/root-password password Jessie
#d-i passwd/root-password-again password Jessie

# User Creation
d-i passwd/make-user boolean true
d-i passwd/user-fullname string Linux User
d-i passwd/username string linuxuser 
d-i passwd/user-password password tux
d-i passwd/user-password-again password tux

# The user account will be added to some standard initial groups. To
# override that, use this.
#d-i passwd/user-default-groups string audio cdrom video

# Software Selections
########################################

# debconf priority in the target system
debconf debconf/priority select critical

# Choose software to install:
tasksel tasksel/first multiselect standard, xfce-desktop, ssh-server, laptop
d-i pkgsel/include string puppet sudo git vim

# allow ctrl+alt+backspace to terminate the X server
keyboard-configuration keyboard-configuration/ctrl_alt_bksp boolean true

# disable predictable network interface names
d-i debian-installer/add-kernel-opts string net.ifnames=0

# remove the spare partition
d-i preseed/late_command string \
  in-target apt-get install -y grub-pc-bin grub-efi-amd64-bin; \
  sdadisk=$(ls /dev | grep 'sda'); \
  nvmedisk=$(ls /dev | grep 'nvme0n1'); \
  emmcdisk=$(ls /dev | grep 'mmcblk0'); \
  if [ "$nvmedisk" ]; \
  then \
    echo -e "d\n5\nw\n" | fdisk /dev/nvme0n1; true; \
    in-target grub-install --target=i386-pc /dev/nvme0n1; \
  elif [ "$sdadisk" ]; \
  then \
    echo -e "d\n5\nw\n" | fdisk /dev/sda; true; \
    in-target grub-install --target=i386-pc /dev/sda; \
  elif [ "$emmcdisk" ]; \
  then \
    echo -e "d\n5\nw\n" | fdisk /dev/mmcblk0; true; \
    in-target grub-install --target=i386-pc /dev/mmcblk0; \
  fi; \
  in-target git clone https://notabug.org/LiohMoeller/installbox.git /tmp/installbox; \
  in-target puppet apply /tmp/installbox/puppet/installbox/site.pp --modulepath=/tmp/installbox/puppet/installbox/modules --verbose --debug --color false; \
  rm -rf /target/tmp/installbox/; \
  exit 0;
